package com.lyf.boot.service.impl;

import com.lyf.boot.model.OssConfigProperties;
import com.lyf.boot.service.IFileStorageService;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.platform.FileStorage;
import org.springframework.stereotype.Service;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Luyufan
 * @date 2023/8/15 10:55
 */
@Service
@RequiredArgsConstructor
public class FileStorageServiceImpl implements IFileStorageService {

    private final FileStorageService fileStorageService;

    @Override
    public void addFileStorage(OssConfigProperties ossConfigProperties){
        //获得存储平台 List
        CopyOnWriteArrayList<FileStorage> list = fileStorageService.getFileStorageList();
        list.add(ossConfigProperties.build());
    }

    @Override
    public void removeFileStorage(String platform) {
        CopyOnWriteArrayList<FileStorage> list = fileStorageService.getFileStorageList();
        FileStorage fileStorage = fileStorageService.getFileStorage(platform);
        if(fileStorage == null){
            return;
        }
        list.remove(fileStorage);
        fileStorage.close();
    }
}
